//
//  CourseListViewController.m
//  TMS
//
//  Created by Riddhi Joshi on 30/01/15.
//  Copyright (c) 2015 Riddhi Joshi. All rights reserved.
//

#import "CourseListViewController.h"

@interface CourseListViewController ()

@property (nonatomic, strong) NSArray *arrCourseInfo;

-(void)loadData;

@end

@implementation CourseListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    NSLog(@"In CourseListViewController trainee id is=%d",self.TraineeId);
    
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
       return self.arrCourseInfo.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    
    
    NSInteger indexOfCourseName = [self.TraineeCourse.db.arrColumnNames indexOfObject:@"coursename"];
    NSInteger indexOfPlatform = [self.TraineeCourse.db.arrColumnNames indexOfObject:@"platform"];
    NSInteger indexofDate=   [self.TraineeCourse.db.arrColumnNames indexOfObject:@"date"];
    
    cell.textLabel.text = [NSString stringWithFormat:@"%@", [[self.arrCourseInfo objectAtIndex:indexPath.row] objectAtIndex:indexOfCourseName]];

    
    cell.detailTextLabel.text=[NSString stringWithFormat:@"%@ %@",[[self.arrCourseInfo objectAtIndex:indexPath.row] objectAtIndex:indexOfPlatform],[[self.arrCourseInfo objectAtIndex:indexPath.row] objectAtIndex:indexofDate]];
    return cell;
}

-(void)loadData{

    
    // Get the results.
    if (self.arrCourseInfo != nil) {
        self.arrCourseInfo = nil;
    }
    
    
    NSDate *date=[NSDate date];
    NSDateFormatter *dateformat=[[NSDateFormatter alloc]init];
    [dateformat setDateFormat:@"dd/MM/yyyy"];
    NSString *dateInString=[dateformat stringFromDate:date];
    
    NSLog(@"Current date %@",dateInString);
    
    
    self.TraineeCourse=[[TraineeCourseTable alloc]init];
    self.arrCourseInfo=[[NSArray alloc]init];
    self.arrCourseInfo=[self.TraineeCourse LoadCoursesWithTraineeIdAlreadyDone:self.TraineeId andDate:dateInString andStatus:1];
    
    NSUInteger count = self.arrCourseInfo.count;
    
    
    if (count<=0) {
        UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"Message" message:@"No courses completed previously" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        alert.alertViewStyle = UIAlertViewStyleDefault;
        [alert show];
        
    }
    else
    {
        // Reload the table view.
        [self.ListTabelView reloadData];
    }
    
}


@end
